// Copyright (c) 2019 Presto Labs Pte. Ltd.
// Author: jaewon

#include "coin1/base/exception_util.h"

#include <glog/logging.h>
#include <gtest/gtest.h>

TEST(ExceptionUtil, StacktraceStripStack) {
  auto msg_1 = detail::Stacktrace(false);
  EXPECT_TRUE(msg_1.find("Stacktrace") != std::string::npos);

  auto msg_2 = detail::Stacktrace(true);
  EXPECT_TRUE(msg_2.find("Stacktrace") == std::string::npos);
}

TEST(ExceptionUtil, ThrowMacro) {
  EXPECT_THROW({ THROW(std::invalid_argument) << "Some error"; }, std::invalid_argument);
}

TEST(ExceptionUtil, CheckMacro) {
  EXPECT_NO_THROW({ CHECK_THROW(true, std::logic_error); });
  EXPECT_THROW({ CHECK_THROW(false, std::logic_error); }, std::logic_error);

  EXPECT_NO_THROW({ CHECK_EQ_THROW(1, 1, std::logic_error); });
  EXPECT_THROW({ CHECK_EQ_THROW(1, 2, std::logic_error); }, std::logic_error);

  EXPECT_NO_THROW({ CHECK_NE_THROW(std::string("1"), "2", std::logic_error); });
  EXPECT_THROW({ CHECK_NE_THROW(std::string("1"), "1", std::logic_error); }, std::logic_error);

  EXPECT_NO_THROW({ CHECK_LE_THROW(2, 3, std::logic_error); });
  EXPECT_NO_THROW({ CHECK_LE_THROW(2, 2, std::logic_error); });
  EXPECT_THROW({ CHECK_LE_THROW(2, 1, std::logic_error); }, std::logic_error);

  EXPECT_NO_THROW({ CHECK_LT_THROW(2, 3, std::logic_error); });
  EXPECT_THROW({ CHECK_LT_THROW(2, 2, std::logic_error); }, std::logic_error);

  EXPECT_NO_THROW({ CHECK_GE_THROW(2, 1, std::logic_error); });
  EXPECT_NO_THROW({ CHECK_GE_THROW(2, 2, std::logic_error); });
  EXPECT_THROW({ CHECK_GE_THROW(2, 3, std::logic_error); }, std::logic_error);

  EXPECT_NO_THROW({ CHECK_GT_THROW(2, 1, std::logic_error); });
  EXPECT_THROW({ CHECK_GT_THROW(2, 2, std::logic_error); }, std::logic_error);
}
